package com.rimi.dao.impl;

import com.rimi.bean.MenuBean;
import com.rimi.dao.MenuDao;
import com.rimi.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MenuDaoImpl extends JdbcUtil implements MenuDao {

    private MenuBean parseResultSetToMenu(ResultSet resultSet) throws SQLException {
        MenuBean menuBean=new MenuBean();
        menuBean.setMenuId(resultSet.getInt(1));
        menuBean.setMenuName(resultSet.getString(2));
        menuBean.setMenuUrl(resultSet.getString(3));
        menuBean.setMenuParentId(resultSet.getInt(4));
        menuBean.setMenuStatus(resultSet.getInt(5));
        return menuBean;
    }

    @Override
    public List<MenuBean> selectMenuList() {
        String sql="select * from  tb_menu where menu_status = 0";
        ResultSet resultSet = executeQuerySql(sql);
        List<MenuBean> menuBeanList=new ArrayList<>();
        try {
            while (resultSet.next()){
                MenuBean menuBean = parseResultSetToMenu(resultSet);
                menuBeanList.add(menuBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeRes();
        }
        return menuBeanList;
    }

    @Override
    public List<MenuBean> selectMenuListByRoleId(Integer roleId) { // or 1=1
        String sql="select m.* from  tb_menu m inner join tb_role_menu rm on m.menu_id = rm.menu_id  where m.menu_status = 0 and rm.role_id = ?";
        Object[] params={roleId};
        ResultSet resultSet = executeQuerySql(sql,params);
        List<MenuBean> menuBeanList=new ArrayList<>();
        try {
            while (resultSet.next()){
                MenuBean menuBean = parseResultSetToMenu(resultSet);
                menuBeanList.add(menuBean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeRes();
        }
        return menuBeanList;
    }


}
